package com.open.capacity.controller;

import com.github.pagehelper.PageInfo;
import com.open.capacity.common.web.Result;
import com.open.capacity.model.OrderAndStation;
import com.open.capacity.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: y_022
 * @date: 9:27-2021/6/25
 * @description:关于用户订单的controller！
 */
@Slf4j
@Api(tags = "ORDER API")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 根据用户id给用户下单
     *
     * @param orderModel
     * @return
     */
    @PostMapping("/addOrder")

    public Result addOrder(OrderAndStation orderModel) {
        try {
            OrderAndStation newOrder = orderService.addOrder(orderModel);
            return Result.succeed(newOrder, "操作成功！");
        } catch (
                Exception e) {
            log.error("下单异常！", e);
        }
        return Result.failed("操作失败！");
    }


    /**
     * 查询用户已支付的订单，查询条件：订单状态为-->1*
     *
     * @param orderModel
     * @return
     */
    @Cacheable(value = "Result", key = "'paidorder'+#orderModel.userid")
    //加入方法返回值加到缓存中。执行顺序：会先从缓存中查询指定key，如果没有命中就执行方法，将方法返回值放入缓存，如果检测命中，则不会执行方法。
    @GetMapping("/findPaidOrder")
    public Result findPaidOrder(OrderAndStation orderModel) {
        try {
            PageInfo<OrderAndStation> orderModelList = orderService.findPaidOrder(orderModel);
            return Result.succeed(orderModelList, "操作成功！");
        } catch (Exception e) {
            log.error("用户已支付订单列表查询失败！", e);
        }
        return Result.failed("操作失败");
    }

    /**
     * 查询未支付订单，查询条件：订单状态为-->0
     *
     * @param orderModel
     * @return
     */
    @GetMapping("/findUnpaidOrder")
    public Result findUnpaidOrder(OrderAndStation orderModel) {
        try {
            List<OrderAndStation> orderModelList = orderService.findUnpaidOrder(orderModel);
            return Result.succeed(orderModelList, "操作成功！");
        } catch (Exception e) {
            log.error("用户未支付订单列表查询失败！", e);
        }
        return Result.failed("操作失败！");
    }

    /**
     * 取消订单
     * 逻辑：按订单id直接删除订单
     *
     * @param orderModel
     * @return
     */
    @PostMapping("/cancelOrder")
    public Result cancelOrder(OrderAndStation orderModel) {
        try {
            orderService.cancelOrder(orderModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
            log.error("取消订单异常！", e);
        }
        return Result.failed("操作失败！");
    }

    /**
     * 支付成功后调用！！
     *
     * @param orderModel
     * @return
     */
    @PostMapping("/completePay")
    //对已支付订单操作时，需要清除缓存，下次请求查询已支付订单时，检测没有此缓存，会从数据库取出来放入缓存，达到缓存更新目的。
    @CacheEvict(value = "Result", key = "'findPaidOrder'")
    public Result completePay(OrderAndStation orderModel) {
        try {
            orderService.completePay(orderModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
            log.error("支付处理异常！", e);
        }
        return Result.failed("操作失败！");
    }

    /**
     * 订单续租
     *
     * @param orderModel
     * @return
     */
    @PostMapping("/leaseRenewal")
    public Result leaseRenewal(OrderAndStation orderModel) {
        try {
            orderService.leaseRenewal(orderModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
            log.error("续租失败！");
        }
        return Result.failed("操作失败！");
    }

//    @PostMapping("/updateOrder")
//    public Result updateOrder(OrderAndStation){
//
//    }

    /**
     * 根据用户id查询用户的所有订单列表
     * <p>
     * todo 暂时废弃，没有应用场景
     *
     * @param orderModel
     * @return
     */
    @GetMapping("/findOrderByUserId")
    public Result findOrderByUserId(OrderAndStation orderModel) {
        try {
            List<OrderAndStation> orderModelList = orderService.findOrderByUserId(orderModel);
            return Result.succeed(orderModelList, "操作成功！");
        } catch (Exception e) {
            log.error("用户订单列表查询失败！", e);
        }
        return Result.failed("操作失败！");
    }

    /**
     * 根据订单id查询工位
     *
     * @param orderAndStation
     * @return
     */
    @GetMapping("/findStationByOrdeid")
    public Result findStationByOrdeid(OrderAndStation orderAndStation) {
        try {
            OrderAndStation res = orderService.findStationByOrdeid(orderAndStation);
            return Result.succeed(res, "操作成功！");
        } catch (Exception e) {
            log.error("操作失败！", e);
        }
        return Result.failed("操作失败！");
    }

    @PostMapping("/findUsingOrder")
    public Result findUsingOrder(OrderAndStation orderAndStation) {
        try {
            List<OrderAndStation> orderModelList = orderService.findUsingOrder(orderAndStation);
            return Result.succeed(orderModelList, "操作成功！");
        } catch (Exception e) {
            log.error("UsingOrder查询失败！", e);
        }
        return Result.failed("操作失败！");
    }

    @PostMapping("/findInvalidationOrder")
    public Result findInvalidationOrder(OrderAndStation orderAndStation) {
        try {
            List<OrderAndStation> orderModelList = orderService.findInvalidationOrder(orderAndStation);
            return Result.succeed(orderModelList, "操作成功！");
        } catch (Exception e) {
            log.error("InvalidationOrder查询失败！", e);
        }
        return Result.failed("操作失败！");
    }

    @GetMapping("/over")
    public void over(String orcode) {
        orderService.orderTimeEnd(orcode);
    }
}
